#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: V1Task.py 
@version:
@time: 2022/03/10 
"""


class V1TaskDataCopy:

    def __init__(self):
        # self.name = name
        from TapClientCore.T03_api_body_templates.v2_api_body import TASK_CREATE_DATA_COPY_ATTRS_ATTRS_TASK_SETTING_DATA
        self.setting = TASK_CREATE_DATA_COPY_ATTRS_ATTRS_TASK_SETTING_DATA.copy()
        from TapClientCore.T04_logic.Dag02.Dag.V1DagDataCopy import V1DagDataCopy
        self.dag = V1DagDataCopy()
        self.stage = None

    def _clone(self, stage):
        self.stage = stage
        return self

    def config(self, setting):
        self.setting.update(setting)

    def readFrom(self, source):
        self.setting['syncPoints'][0]['connectionId'] = source.node.get('connectionId')
        return self._clone(source)

    def writeTo(self, sink, relation):
        self.dag.edge(self.stage, sink, relation, self.setting)

    @property
    def task(self):
        attr = {
            # 'name': self.name,
            'attrs': self.dag.attrs,
            'dag': self.dag.dag,
        }
        attr.update(self.setting)
        return attr


if __name__ == '__main__':
    from pprint import pprint
    from TapClientCore.T04_logic.Dag02.node.data_copy_source_node import DataCopySourceNode
    from TapClientCore.T04_logic.Dag02.node.data_copy_target_node import DataCopySinkNode
    from TapClientCore.T04_logic.Dag02.relations.relation import TaskMapTable

    S = DataCopySourceNode('auto_mysql')
    T = DataCopySinkNode('auto_mysql')
    d = V1TaskDataCopy()
    d.readFrom(S).writeTo(T, TaskMapTable({'syncObjects': {"objectNames": ["my2"], "type": "table"}}))
    # pprint(d.task)

    from TapClientCore.T03_api_body_templates.v2_api_body import TASK_CREATE_DATA_COPY

    t = TASK_CREATE_DATA_COPY.copy()
    t.update(d.task)
    pprint(t)
